public class GetIntersectionNode {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {//leecode 160 求相交链表
        ListNode pl=headA;
        ListNode ps=headB;
        int lengthA=length(headA);
        int lengthB=length(headB);
        if(lengthA>=lengthB){
            int temp=lengthA-lengthB;
            while(temp!=0){
                headA=headA.next;
                temp--;
            }
        }else{
            int temp=lengthB-lengthA;
            while(temp!=0){
                headB=headB.next;
                temp--;
            }
        }
        while(headA!=null&&headB!=null){
            if(headA==headB){
                return headA;
            }else{
                headA=headA.next;
                headB=headB.next;
            }
        }
        return null;
    }

    public int length(ListNode head){
        int length=0;
        while(head!=null){
            head=head.next;
            length++;
        }
        return length;
    }
}
